package xyz.xpman.prisonerfitness.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.cache.AsyncImageLoader;
import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.entity.Guide;
import xyz.xpman.prisonerfitness.entity.Mappings;
import xyz.xpman.prisonerfitness.entity.Moves;
import xyz.xpman.prisonerfitness.entity.Training;

public class TrainingGuideActivity extends BaseActivity {
    private Moves moves;
    private TextView trainingPart;
    private TextView trainingPoint;

    private ImageView movesGuidImg;
    private ImageView viewVideoImg;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_guide);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        initData();
        setTitle(moves.getTitle());

        initView();
        initEvent();

        processView();
    }

    private void initData() {
        moves = Mappings.getMove(getIntentExtra(Constant.EXTRA_MOVES_ID));
    }


    private void initView() {
        trainingPart = findViewById(R.id.training_part);
        trainingPoint = findViewById(R.id.training_point);
        movesGuidImg = findViewById(R.id.moves_guide_img);

        viewVideoImg = findViewById(R.id.view_video);
    }


    private void initEvent() {
        findViewById(R.id.start_training).setOnClickListener(view -> {
            Intent intent = newIntent(TrainingStandardActivity.class);
            intent.putExtra(Constant.EXTRA_MOVES_ID, moves.getId());
            startActivity(intent);
        });
    }

    private void processView() {
        final Training training = moves.getTraining();

        // 训练部位
        addIndentation(trainingPart, training.getPart(), 2);
        // 训练要点
        trainingPoint.setText(training.getPoints());



        final Guide guide = training.getGuide();

        // 引导图
        if (guide.hasImg()) {
            Bitmap bitmap = AsyncImageLoader.get(guide.getImg());

            if (null != bitmap) {
                movesGuidImg.setImageBitmap(bitmap);
            }
        }


        Log.i("TrainingGuide", "video: " + guide.getVideo());

        // 视频讲解资源处理
        if (guide.hasVideo()) {
            viewVideoImg.setVisibility(View.VISIBLE);

            viewVideoImg.setOnClickListener(view-> {
                Intent intent = newIntent(TrainingGuideVideoActivity.class);
                intent.putExtra(Constant.EXTRA_VIDEO, guide.getVideo());
                startActivity(intent);
            });
        } else {
            viewVideoImg.setVisibility(View.GONE);
        }
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.training_guide_menu, menu);

        // 训练音乐设置
        menu.findItem(R.id.set_training_music).setOnMenuItemClickListener(view-> {
            Intent intent = newIntent(TrainingMusicSettingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
        });

        // 间隔设置
        menu.findItem(R.id.set_training_time_interval).setOnMenuItemClickListener(view-> {
            Intent intent = newIntent(TrainingTimeIntervalActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
        });

        return true;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }


}